import { computed, ref } from 'vue'
import { defineStore } from 'pinia'

// 选项式
export const useCounterStore = defineStore('counter', () => {
  const count = ref(0)
  const doubleCount = computed(() => count.value * 2)

  function increment() {
    count.value++
  }

  return { count, doubleCount, increment }
})

// 组合式
export const useUserStore = defineStore('user', () => {
  const age = ref(27)
  const level = ref(5)
  const account = ref('SD77842')
  const nickname = ref('自古风流')

  return { age, level, account, nickname } // 将数据暴露出去，共享给需要的组件
})

export const useMainStore = defineStore('main', {
  // 创建 store，并暴露出去
  // state: () => {
  //   return { count: 0 }
  // },
  state: () => ({ counter: 1 }), // 共享的数据
  getters: {
    //通过计算得到的共享数据
    doubleCount: () => this.state.counter * 2,
    doubleCountPlusOne() {
      return this.counter + 1
    },
  },
  actions: {
    //共享的函数
    increment() {
      this.counter++
    },
  },
})
